/*
 * Copyright 2017 yetao &lt;yetao_ustc@163.com&gt;.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package date.yetao.maven.all.mybatis;

import java.util.List;
import java.util.Properties;
import org.mybatis.generator.api.IntrospectedColumn;

import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

/**
 * 增加deleteUseFlag方法的插件
 * <P>
 *
 * @author yetao &lt;yetao_ustc@163.com&gt;
 */
public class DeleteUseFlagPlugin extends PluginAdapter {

    /**
     * 标记删除的字段
     */
    private static final String DEL_FLAG = "del_flag";

    public DeleteUseFlagPlugin() {
        super();
    }

    @Override
    public boolean validate(List<String> warnings) {
        // this plugin is always valid
        return true;
    }

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        // 判断是否需要添加deleteUseFlag方法
        List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();
        if (allColumns == null || allColumns.isEmpty()) {
            return true;
        }
        // 判断接口是否为null
        if (interfaze == null) {
            return true;
        }
        for (IntrospectedColumn col : allColumns) {
            if (DEL_FLAG.equalsIgnoreCase(col.getActualColumnName())) {
                // 找到了就添加方法
                Method method = new Method();
                method.setName("deleteUseFlag");
                method.setReturnType(new FullyQualifiedJavaType("int"));
                List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
                IntrospectedColumn priCol = primaryKeyColumns.get(0);
                Parameter para = new Parameter(priCol.getFullyQualifiedJavaType(), priCol.getJavaProperty());
                method.addParameter(para);
                
                // 添加注释
                method.addJavaDocLine("/**");
                method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
                method.addJavaDocLine(" * This method corresponds to the database table "+introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
                method.addAnnotation(" *");
                method.addAnnotation(" * @mbg.generated");
                method.addAnnotation(" */");
                interfaze.addMethod(method);

                return true;
            }
        }
        return true;
    }

    /**
     * 增加deleteUseFlag的Mapper
     * <P>
     *
     * @param document
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean sqlMapDocumentGenerated(Document document,
            IntrospectedTable introspectedTable) {
        // 判断是否需要添加deleteUseFlag方法
        List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();
        if (allColumns == null || allColumns.isEmpty()) {
            return true;
        }
        for (IntrospectedColumn col : allColumns) {
            if (DEL_FLAG.equalsIgnoreCase(col.getActualColumnName())) {
                // 找到了就添加Mapper
                XmlElement root = document.getRootElement();
                XmlElement del = new XmlElement("update");
                del.addAttribute(new Attribute("id", "deleteUseFlag"));
                List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
                IntrospectedColumn priCol = primaryKeyColumns.get(0);
                String colName = priCol.getActualColumnName();
                del.addAttribute(new Attribute("parameterType", priCol.getFullyQualifiedJavaType().getFullyQualifiedName()));
                del.addElement(new TextElement("<!--"));
                del.addElement(new TextElement("  WARNING - @mbg.generated"));
                del.addElement(new TextElement("  This element is automatically generated by MyBatis Generator, do not modify."));
                del.addElement(new TextElement("-->"));
                del.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
                del.addElement(new TextElement("set " + DEL_FLAG + " = 1"));
                del.addElement(new TextElement("where " + colName + " = #{" + priCol.getJavaProperty() + ",jdbcType=" + priCol.getJdbcTypeName() + "}"));

                root.addElement(del);
                return true;
            }
        }
        return true;
    }

}
